package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.logiarealine.Logiarealine;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.logiarealine.ILogiarealineService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物流运作区域明细信息Controller
 * Created by weih on 2016-10-14.
 */
@Controller
@RequestMapping("/mdm/logiarealine")
public class LogiarealineController extends BaseController {

    @Autowired
    ILogiarealineService logiarealineService;
    /**
     * 跳转新增物流运作区域明细页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/logiarealine/add";
    }
    /**
     * 新增物流运作区域明细信息
     * @param logiarealine 物流运作区域明细对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addLogiarealine(@RequestBody Logiarealine logiarealine,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiarealine.setCreateUser(getSessionUser().getUserId());
        logiarealine.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiarealineService.addLogiarealine(logiarealine);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑物流运作区域明细页面
     * @param logiarealineCode  物流运作区域明细代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String logiarealineCode,Model model){
        model.addAttribute("logiarealine", logiarealineService.getLogiarealineByCode(logiarealineCode));
        return "mdm/logiarealine/edit";
    }

    /**
     * 编辑物流运作区域明细信息
     * @param logiarealine
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editLogiarealine(@RequestBody Logiarealine logiarealine,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Logiarealine oldLogiarealine=logiarealineService.getLogiarealineByCode(logiarealine.getLogiarealineCode());
        oldLogiarealine.setId(logiarealine.getId());
        oldLogiarealine.setLogisticsAreaId(logiarealine.getLogisticsAreaId());
        oldLogiarealine.setLogisticsAreaCode(logiarealine.getLogisticsAreaCode());
        oldLogiarealine.setLogisticsAreaLineLevel(logiarealine.getLogisticsAreaLineLevel());
        oldLogiarealine.setLogisticsPointId(logiarealine.getLogisticsPointId());
        oldLogiarealine.setLogisticsPointCode(logiarealine.getLogisticsPointCode());
        oldLogiarealine.setCountryCode(logiarealine.getCountryCode());
        oldLogiarealine.setProvinceCode(logiarealine.getProvinceCode());
        oldLogiarealine.setCityCode(logiarealine.getCityCode());
        oldLogiarealine.setCountyCode(logiarealine.getCountyCode());
        oldLogiarealine.setUpdateUser(getSessionUser().getUserId());
        oldLogiarealine.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiarealineService.updateLogiarealine(oldLogiarealine);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除物流运作区域明细信息
     * @param logiarealine 物流运作区域明细对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteLogiarealine(@RequestBody Logiarealine logiarealine,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiarealine.setUpdateUser(getSessionUser().getUserId());
        logiarealine.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logiarealineService.deleteLogiarealine(logiarealine);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

	/**
     * 跳转物流运作区域明细明细页面
     * @param logiarealineCode  大区代码
     */
    @RequestMapping("/detail")
    public String toDetailPage(String logiarealineCode,Model model){
        Logiarealine logiarealine = logiarealineService.getLogiarealineByCode(logiarealineCode);
        model.addAttribute("logiarealine", logiarealine);

        model.addAttribute("nation", regionService.getRegionByCode(logiarealine.getNationCode()));
        return "mdm/logiarealine/detail";
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/logiarealine/list";
    }

    /**
     * 分页查询数据
     * @param logiarealine 物流运作区域明细对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Logiarealine> getLogiarealineData(Logiarealine logiarealine ,DataTableRequest dtrequest){
        int cnt = (int)logiarealineService.getLogiarealineCount(logiarealine);
        List<Logiarealine> list = logiarealineService.queryLogiarealines(logiarealine, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Logiarealine> result = new PaginationResult<Logiarealine>(list, pi);
        return new DataTableResponse<Logiarealine>(dtrequest.getsEcho(),result);
    }

}
